<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head> 
   <meta charset="UTF-8">
   <%@ include file="/WEB-INF/views/include/head.jsp"%>
   <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
   <link rel="stylesheet" href="${ctxStatic}/css/layerSm.css">
   <script src="${ctxStatic}/js/jquery-1.9.1.min.js"></script>
   <script src="${ctxStatic}/js/layer.js"></script>
   <script src="${ctxStatic}/js/style.js"></script>
   <script src="${ctxStatic}/jquery-barcode/jquery-barcode.js" type="text/javascript"></script>
   <script type="text/javascript">
	$(document).ready(function(){
		$("#venderId").select2();
		$("#brandId").select2();
		showBtnRequestMaterial();
		$("#inventoryRequestBarCodeDiv").empty().barcode($("#stockBarCode").val(), "code128",{barWidth:2, barHeight:30,showHRI:false});
	});
	
	// 申请原辅料按钮制御
	function showBtnRequestMaterial(){
		var pVender = $("#venderId").val();
		if(pVender != '' && pVender != null && pVender != "undefined"){
			$("#btnRequestMaterial").show();
		}else{
			$("#btnRequestMaterial").hide();
		}
	}
	// 客户联动
				var sequenceReturnCode = '${sequenceReturnCode}';
	function venderSelFun(t){
		showBtnRequestMaterial();
		
		if($("#venderId").val() != '' && $("#venderId").val() != null && $("#venderId").val() != "undefined"){
				var simpleName=$(t).find("option:selected").attr("data-value");
				if(simpleName!=null && simpleName!=''){
					simpleName = simpleName.toUpperCase();
				}
				// 客户简称4位年月日时分秒
				$("#inventoryRequestNo").val("I."+simpleName + sequenceReturnCode);
			
			// 客户品牌联动
			var param = '';
			var paramVenderId = $("#venderId").val();
			param={"venderId":paramVenderId};
	        $.post("${ctx}/inventory/request/brand",param,function(result){            
	            var html="<option value=''><spring:message code="customer_pleaseChoose"/></option>";
	            $.each(result,function(i,e){
	                html+="<option value='"+e.brandId+"'>"+e.brandName+"</option>";
	            });
	            $("#brandId").html(html);
	            $("#brandId").select2();
	        },'json');
		}else{
			$("#inventoryRequestNo").val('');
			var htmlEmpty="<option value=''><spring:message code="customer_pleaseChoose"/></option>";
            $("#brandId").html(htmlEmpty);
            $("#brandId").select2();
		}
		
		delAllDetails();
	}
	
	function pad2(n) { return n < 10 ? '0' + n : n }
	
	// 返回年月日时分秒
	function generateTimeReqestNumber() {
	    var date = new Date();
	    return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) + pad2(date.getMinutes()) + pad2(date.getSeconds());
	}
	
    // 删除明细
    function deleteInventoryRequestDetials(trIndex){
    	$("#trIndex").val(trIndex);
    	// 格式化，去除逗号
        formUnFormat($("#inputForm"));
 	    var params = $("#inputForm").serialize();
        // 调用后台更新数据
        $.post("${ctx}/inventory/request/delRequestDetails",params,function(result) {
            // 回调函数赋值
            var h = $(callBackHtml(result)).find("#tableRequestDetails").html();
            $('#tableRequestDetails').html(h);
        }, 'html');
    }
    
    //保存方法
    var saveForm={
         t:null,
         save:function(t){
        	 
        	 if($("#tableRequestDetails tr").length==0){
        		 layer.msg('<spring:message code="inventory_request_material_details_empty"/>', {time:1000});
        		 return;
        	 }
        	 
              $(t).attr("disabled",true);
              this.t=t;
              // 表单验证
              $("#inputForm").validate({
              fail: function(element,e) {  
                  $(saveForm.t).attr("disabled",false);
              },
                   submitHandler: function() {
                	   $('#inventoryCategory').attr('disabled',false);
                   	   // 格式化，去除逗号
                       formUnFormat($("#inputForm"));
                        // 从表单获取数据信息
                        var params = $("#inputForm").serialize();
                        // 调用后台保存方法
                        $.post("${ctx}/inventory/request/save",params,function(result){
                             // 提示信息
                             tipParent(result.message);
                             // 返回状态
                             if(result.statusCode==STATUSCODEOK){
                                 // 刷新页面
                                 window.parent.frames[0].searchListForm();
                                 closeWin();
                             }else{
                                  $(saveForm.t).attr("disabled",false);
                             }
                        },'json'); 
                   }
              });
              // 提交表单
              $("#inputForm").submit(); 
         } 
    }
    
	// 显示选择物料
	function showInventoryMaterial(){
		popup('1230px','680px','${ctx}/common/material/list?venderId=' + $('#venderId').val() + '&brandId=' + $('#brandId').val());
	}
    
    // 选择物料回调
    function selectMaterialCallBack(selectIndex,objMaterials){
    	if(selectIndex==null || selectIndex==''){
    		return;
    	}
    	
    	// 分割选择id
    	strSelects = selectIndex.split(",");
    	var materialIndex = $("#tableRequestDetails tr").length;
    	for (i=0;i<objMaterials.length ;i++ ){
    		 for(j=0;j<strSelects.length;j++){
    			  var idx = parseInt(strSelects[j]);
                  if(i==idx){
		    		 if(!isMaerialRepeate(objMaterials[i].materialId)){
		    			 $("#tableRequestDetails").append('<tr>' +
		    					                          '<input type="hidden" class="subMaterialId" name="requestDetailsList[' + materialIndex + '].materialId" value="' + objMaterials[i].materialId + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].filePath" value="' + objMaterials[i].fileName + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialNameCn" value="' + objMaterials[i].materialNameCn + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialCode" value="' + objMaterials[i].materialCode + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialSpec" value="' + objMaterials[i].materialSpec + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialGramWeight" value="' + objMaterials[i].materialgramWeight + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialComposition" value="' + objMaterials[i].materialComposition + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialThreadCount" value="' + objMaterials[i].materialThreadCount + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialStyle" value="' + objMaterials[i].materialStyle + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialColor" value="' + objMaterials[i].materialColor + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].materialWeave" value="' + objMaterials[i].materialWeave + '"/>' +
		    					                          '<input type="hidden" name="requestDetailsList[' + materialIndex + '].brandName" value="' + objMaterials[i].brandNameCn + '"/>' +
		    			                                   '</tr>');
		    		 	materialIndex++;
		    		 }
                  }    			 
    		 }
    	}
 
    	formUnFormat($("#inputForm"));
 	    var params = $("#inputForm").serialize();
        // 调用后台更新数据
        $.post("${ctx}/inventory/request/addRequestDetails",params,function(result) {
            // 回调函数赋值
            var h = $(callBackHtml(result)).find("#tableRequestDetails").html();
            $('#tableRequestDetails').html(h);
        }, 'html');
    	
    }
    
    // 原辅料重复
    function isMaerialRepeate(itemValue) {

         var number = 0;
         // 从页面获取值 进行比较
         $("#tableRequestDetails tr").each(function(j){
            if (itemValue == $(this).find('.subMaterialId').val()) {
                 number = number+1;
          }
         });
        
         if (number> 0) {
            return true;
         } else {
            return false;
         }
    }
    
    function delAllDetails(){
        // 调用后台更新数据
        $.post("${ctx}/inventory/request/delAllDetails",'',function(result) {
            // 回调函数赋值
            var h = $(callBackHtml(result)).find("#tableRequestDetails").html();
            $('#tableRequestDetails').html(h);
        }, 'html');
    }
    
    function funFormatNumber(obj){
    	
    	var intReg = /^[0-9]+$/;   
    	var floatReg = /^[0-9]+.?[0-9]*$/;   
    	var s = $(obj).val();
        
    	// 整数
    	if(intReg.test(s)){
    		var numberTen = /^[0-9]{1,7}$/;
			if (!numberTen.test(s)){
				$(obj).val('');
			}
		// 小数	
    	}else if(floatReg.test(s)){
    		var numberVerification = /^[0-9]{1,7}.?[0-9]{0,3}$/;  
			if (!numberVerification.test(s)){
				$(obj).val('');
			}
    	}else{
    		$(obj).val('');
    	}
    	formatNumber(obj);
    }
	</script>
</head>
<body>
<div class="layerHeader clearfix">
	<c:choose>
		<c:when test="${inventoryRequest.id != null && inventoryRequest.id != ''}">
			<p class="headerTitle"><spring:message code="inventory_request_edit_title"/></p>
		</c:when>
		<c:otherwise>
			<p class="headerTitle"><spring:message code="inventory_request_create_title"/></p>
		</c:otherwise>
	</c:choose>
    <div class="headerButton clearfix">
    	<!-- 申请原辅料按钮 -->
        <button type="button" id="btnRequestMaterial" onclick="showInventoryMaterial()"><spring:message code="inventory_request_create_btn_material"/></button>
        <!-- 保存 -->
        <button type="button" onclick="saveForm.save(this)"><spring:message code="common_save"/></button>
        <!-- 取消 -->
        <button onclick="closeWin()"><spring:message code="btn_cancle"/></button>
    </div>
</div>
<form id="inputForm" method="post">
<c:set value="<%=ResourcesUtil.getConfig(\"inventory_category\")%>" var="varInventoryCategory" />
<input type="hidden" id="id" name="id" value="${inventoryRequest.id}"/>
<input type="hidden" id="trIndex" name="trIndex" value="${inventoryRequest.trIndex}"/>
<div class="smOrderBox">
    <div class="items clearfix">
        <div class="col-33 clearfix">
            <!-- 入库单号 -->
            <p class="tit"><spring:message code="inventory_request_create_requestNo"/></p>
            <input style="background-color:inherit;color:#333;" type="text" id="inventoryRequestNo" name="inventoryRequestNo" value="${inventoryRequest.inventoryRequestNo}" readonly="true">
        </div>
        <div class="col-33 clearfix">
            <!-- 入库单条码 -->
            <p class="tit"><spring:message code="inventory_request_create_barCode"/></p>
            <div id="inventoryRequestBarCodeDiv"></div>
            <input type="hidden" id="stockBarCode" name="stockBarCode" value="${inventoryRequest.stockBarCode}">
            
        </div>      
    </div>
    <div class="items clearfix">
        <div class="col-33 clearfix haveToBox">
            <!-- 客户名称 -->
            <p class="tit"><spring:message code="inventory_request_create_customerName"/></p>
            <select style="width:39.5%" id="venderId" name="venderId" onchange="venderSelFun(this);" data-rule-required="true"  data-msg-required="<spring:message code="contract_error_msg_customerName"/>">
               <option value=""><spring:message code="select_customer"/></option>
               <c:forEach items="${inventoryRequest.venderList}" var="venderInfo">
                  <option data-value="${venderInfo.ognizationSimpleName}" <c:if test="${inventoryRequest.venderId eq venderInfo.id}">selected="selected"</c:if> value="${venderInfo.id}" > 
                     ${venderInfo.ognizationName}
                  </option>
               </c:forEach>
            </select>
            <p class="haveTo">*</p>
        </div>
        <div class="col-33 clearfix">
            <!-- 客户品牌 -->
            <p class="tit"><spring:message code="inventory_request_create_brand"/></p>
            <select style="width:39.5%" id="brandId" name="brandId" onchange="delAllDetails();">
               <option value=""><spring:message code="customer_pleaseChoose"/></option>
               <c:forEach items="${inventoryRequest.brandList}" var="brandInfo">
                  <option <c:if test="${inventoryRequest.brandId eq brandInfo.brandId}">selected="selected"</c:if> value="${brandInfo.brandId}" > 
                     ${brandInfo.brandName}
                  </option>
               </c:forEach>
            </select>
        </div>
        <div class="col-33 clearfix">
            <!-- 入库类别 -->
            <p class="tit"><spring:message code="inventory_request_create_inventoryCategory"/></p>
            <select style="width:39.5%" id="inventoryCategory" name="inventoryCategory" disabled="true">
            <option value=""><spring:message code="customer_pleaseChoose"/></option>
                <c:forEach items="${fns:getDictList(varInventoryCategory)}"   var="inventoryCategoryInfo">
                    <option <c:if test="${fn:split(inventoryRequest.inventoryCategory,'-')[1] eq inventoryCategoryInfo.itemValue}">selected="selected"</c:if> value="${inventoryCategoryInfo.itemCode}-${inventoryCategoryInfo.itemValue}" > 
                        ${inventoryCategoryInfo.itemName}
                    </option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="items clearfix none">
        <div class="col-33 clearfix">
        <p class="tit">&nbsp;</p>
        </div>
        <div class="col-33 clearfix">
            <!-- 申请人 -->
            <p class="tit"><spring:message code="inventory_request_create_requestUser"/></p>
            <p><input type="text" id="requestUser" name="requestUser" value="${inventoryRequest.requestUser}" readonly="true"></p>
        </div>
        <div class="col-33 clearfix">
            <!-- 申请日期 -->
            <p class="tit"><spring:message code="inventory_request_create_requestDate"/></p>
            <p><input type="text" id="requestDate" name="requestDate" value="<fmt:formatDate value="${inventoryRequest.requestDate}" pattern="yyyy-MM-dd" />" readonly="true"></p>
        </div>
    </div>
</div>
<div class="tableArea">
    <div class="tableScrollSm" style="overflow:auto;width:1186px;height:420px;">
        <table cellpadding="0" class="tableHead">
            <thead>
            <tr>
                <!-- 操作 -->
                <th style="width:30px;"><spring:message code="inventory_request_create_details_operate"/></th>
                <!-- 物料图片 -->
                <th class="tab-wltp"><spring:message code="inventory_request_create_details_img"/></th>
                <!-- 名称编码 -->
                <th><p class="tab-wlmcbj"><spring:message code="inventory_request_create_details_nameCode"/></p></th>
                <!-- 幅宽 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_spec"/></p></th>
                <!-- 克重 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_weight"/></p></th>
                <!-- 成分 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_composition"/></p></th>
                <!-- 纱支密度 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_thread_count"/></p></th>
                <!-- 风格 -->
                <th><p class="width-70px"><spring:message code="inventory_request_create_details_style"/></p></th>
                <!-- 颜色 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_color"/></p></th>
                <!-- 织法 -->
                <th><p class="width-80px"><spring:message code="inventory_request_create_details_weave"/></p></th>
                <!-- 类别 -->
                <th><p class="width-70px"><spring:message code="inventory_request_create_details_type"/></p></th>
                <!-- 客户品牌 -->
                <th class="width-100px"><spring:message code="inventory_request_create_details_brand"/></th>
                <!-- 供应商名称 -->
                <th class="width-100px"><spring:message code="inventory_request_create_details_supllier"/></th>
                <!-- 申请入库数量 -->
                <th class="width-80px"><spring:message code="inventory_request_create_details_requestQuantity"/></th>
            </tr>
            </thead>
        </table>
        <table cellpadding="0" class="tableBody" id="tableRequestDetails">
        <c:if test="${not empty inventoryRequest.requestDetailsList}">
            <tbody>
            <c:forEach items="${inventoryRequest.requestDetailsList}" var="requestDetailsInfo" varStatus="status">
            <tr>
                <td style="width: 30px"><img src="${ctxStatic}/images/del_icon.png" class="delBtn" onclick='deleteInventoryRequestDetials(${status.index})'></td>
                <td class="tab-wltp">
                  <c:if test="${not empty requestDetailsInfo.filePath}">
                        <img src="${requestDetailsInfo.filePath}" class="wltp">
                  </c:if>
                  <c:if test="${empty requestDetailsInfo.filePath}">
                        <img src="${ctxStatic}/images/pic-none.jpg" class="wltp">
                  </c:if>
                </td>
                <td><p class="tab-wlmcbj">${requestDetailsInfo.materialNameCn}<br>${requestDetailsInfo.materialCode}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialSpec}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialGramWeight}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialComposition}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialThreadCount}</p></td>
                <td><p class="width-70px">${fns:getDictName(requestDetailsInfo.materialStyle)}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialColor}</p></td>
                <td><p class="width-80px">${fns:getDictName(requestDetailsInfo.materialWeave)}</p></td>
                <td><p class="width-70px">&nbsp;</p></td>
                <td class="width-100px">${requestDetailsInfo.brandName}</td>
                <td class="subDetails width-100px"><input type="text" name="requestDetailsList[${status.index}].supplierName" value="${requestDetailsInfo.supplierName}" maxlength="50"></td>
                <td class="subDetails width-80px"><input type="tel" name="requestDetailsList[${status.index}].requestQuantity" value="<fmt:formatNumber value="${requestDetailsInfo.requestQuantity}" pattern="#,##0.###"/>" maxlength="11" onblur="funFormatNumber(this);" onfocus="unFormat(this)"></td>
                <input type="hidden" class="subMaterialId" name="requestDetailsList[${status.index}].materialId" value="${requestDetailsInfo.materialId}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].filePath" value="${requestDetailsInfo.filePath}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialNameCn" value="${requestDetailsInfo.materialNameCn}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialCode" value="${requestDetailsInfo.materialCode}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialSpec" value="${requestDetailsInfo.materialSpec}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialGramWeight" value="${requestDetailsInfo.materialGramWeight}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialComposition" value="${requestDetailsInfo.materialComposition}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialThreadCount" value="${requestDetailsInfo.materialThreadCount}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialStyle" value="${requestDetailsInfo.materialStyle}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialColor" value="${requestDetailsInfo.materialColor}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].materialWeave" value="${requestDetailsInfo.materialWeave}"/>
                <input type="hidden" name="requestDetailsList[${status.index}].brandName" value="${requestDetailsInfo.brandName}"/>
            </tr>
            </c:forEach>
            </tbody>
        </c:if>    
        </table>
    </div>
</div>
</form>
</body>
</html>